#include <iostream>
#include <ostream>

using namespace std;

/* 复数类 */
class Complex {
    private:
        float real; // 实部
        float img;  // 虚部

    public:
        Complex(): real(0.0), img(0.0) {}
        Complex(float real, float img): real(real), img(img) {}
        void show() const { cout << real << " + " << img << "i" << endl; }

        /* 运算符重载 */
        Complex operator+(const Complex &c); // 重载运算符
        friend Complex operator-(const Complex &c1, const Complex &c2); // 友元函数的形式
        friend Complex operator+=(Complex &c1, const Complex &c2); // 友元函数的形式
        friend bool operator==(const Complex &c1, const Complex &c2); // 友元函数的形式
        friend bool operator!=(const Complex &c1, const Complex &c2); // 友元函数的形式
        Complex operator*(const Complex &c); // 重载运算符
        Complex operator/(const Complex &c); // 重载运算符
        friend ostream &operator<<(ostream &out, const Complex &c);
        friend istream &operator>>(istream &in, Complex &c);
}; 

Complex Complex::operator+(const Complex &c)
{
    Complex res;

    res.real = this->real + c.real;
    res.img = this->img + c.img;

    return res;
}

Complex operator-(const Complex &c1, const Complex &c2)  // 全局函数，不是类的成员
{
    Complex res;

    res.real = c1.real - c2.real;
    res.img = c1.img - c2.img;

    return res;
}

Complex operator+=(Complex &c1, const Complex &c2)  // 全局函数，不是类的成员
{
    c1.real += c2.real;
    c1.img += c2.img;

    return c1;
}

bool operator==(const Complex &c1, const Complex &c2)  // 全局函数，不是类的成员
{
    return (c1.real == c2.real) && (c1.img == c2.img);
}

bool operator!=(const Complex &c1, const Complex &c2)  // 全局函数，不是类的成员
{
    return !((c1.real == c2.real) && (c1.img == c2.img));
}

Complex Complex::operator*(const Complex &c)
{
    Complex res;

    res.real = this->real * c.real - this->img * c.img;
    res.img = this->img * c.real + this->real * c.img;

    return res;
}

Complex Complex::operator/(const Complex &c)
{
    Complex res;

    // 实现公式稍微麻烦

    return res;
}

ostream &operator<<(ostream &out, const Complex &c) // 返回引用类型
{
    out << c.real << " + " << c.img << "i";

    return out; // 返回本身
}

istream &operator>>(istream &in, Complex &c)
{
    in >> c.real >> c.img;

    return in;
}

int main(void)
{
    Complex c1(1, 7);
    Complex c2(2, 5);
    Complex c3, c4, c5;

    c1.show();
    c2.show();

    // 等效于 c3 = c1.operator+(c2);
    c3 = c1 + c2;
    c3.show();

    c3 = c1 - c2; // 友元函数的实现方式，运算符使用方式相同
    c3.show();

    c3 = c1 * c2;
    c3.show();

    c4 += c1;
    c4.show();

    cout << (c4 == c2) << endl;
    cout << (c1 != c2) << endl;

    cin >> c5;
    cout << c5 << endl;

    return 0;
}

